


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: FileUploadController</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">com.bupt.websocket.controller</a> ]
</div>

<h1>Coverage Summary for Class: FileUploadController (com.bupt.websocket.controller)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">FileUploadController</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    50%
  </span>
  <span class="absValue">
    (5/ 10)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    46.3%
  </span>
  <span class="absValue">
    (19/ 41)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;package com.bupt.websocket.controller;
<i>2</i>&nbsp;
<i>3</i>&nbsp;import com.bupt.websocket.bean.WebSocketResult;
<i>4</i>&nbsp;import com.bupt.websocket.service.impl.StorageFileNotFoundException;
<i>5</i>&nbsp;import com.bupt.websocket.service.StorageService;
<i>6</i>&nbsp;import com.bupt.websocket.service.impl.WebPush;
<i>7</i>&nbsp;import com.bupt.websocket.utils.ClientWeb;
<i>8</i>&nbsp;import com.bupt.websocket.utils.IkariUtils;
<i>9</i>&nbsp;import com.jyt.message.Message;
<i>10</i>&nbsp;import com.jyt.util.MySerializable;
<i>11</i>&nbsp;import org.springframework.beans.factory.annotation.Autowired;
<i>12</i>&nbsp;import org.springframework.core.io.Resource;
<i>13</i>&nbsp;import org.springframework.http.HttpHeaders;
<i>14</i>&nbsp;import org.springframework.http.ResponseEntity;
<i>15</i>&nbsp;import org.springframework.stereotype.Controller;
<i>16</i>&nbsp;import org.springframework.ui.Model;
<i>17</i>&nbsp;import org.springframework.web.bind.annotation.*;
<i>18</i>&nbsp;import org.springframework.web.multipart.MultipartFile;
<i>19</i>&nbsp;import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
<i>20</i>&nbsp;import org.springframework.web.servlet.mvc.support.RedirectAttributes;
<i>21</i>&nbsp;
<i>22</i>&nbsp;import java.io.IOException;
<i>23</i>&nbsp;import java.util.stream.Collectors;
<i>24</i>&nbsp;
<i>25</i>&nbsp;import com.bupt.websocket.service.StorageService;
<i>26</i>&nbsp;import com.bupt.websocket.service.impl.StorageFileNotFoundException;
<i>27</i>&nbsp;
<i>28</i>&nbsp;@Controller
<i>29</i>&nbsp;public class FileUploadController {
<i>30</i>&nbsp;
<i>31</i>&nbsp;    private final StorageService storageService;
<i>32</i>&nbsp;
<i>33</i>&nbsp;    @Autowired
<b class="fc"><i>34</i>&nbsp;    public FileUploadController(StorageService storageService) {</b>
<b class="fc"><i>35</i>&nbsp;        this.storageService = storageService;</b>
<b class="fc"><i>36</i>&nbsp;    }</b>
<i>37</i>&nbsp;
<i>38</i>&nbsp;    @GetMapping(&quot;/showUploadFileList&quot;)
<i>39</i>&nbsp;    public String listUploadedFiles(Model model) throws IOException {
<i>40</i>&nbsp;
<b class="nc"><i>41</i>&nbsp;        model.addAttribute(&quot;files&quot;, storageService.loadAll().map(</b>
<b class="nc"><i>42</i>&nbsp;                path -&gt; MvcUriComponentsBuilder.fromMethodName(FileUploadController.class,</b>
<b class="nc"><i>43</i>&nbsp;                        &quot;serveFile&quot;, path.getFileName().toString()).build().toString())</b>
<b class="nc"><i>44</i>&nbsp;                .collect(Collectors.toList()));</b>
<i>45</i>&nbsp;
<b class="nc"><i>46</i>&nbsp;        return &quot;uploadForm&quot;;</b>
<i>47</i>&nbsp;    }
<i>48</i>&nbsp;
<i>49</i>&nbsp;    @GetMapping(&quot;/files/{filename:.+}&quot;)
<i>50</i>&nbsp;    @ResponseBody
<i>51</i>&nbsp;    public ResponseEntity&lt;Resource&gt; serveFile(@PathVariable String filename) {
<i>52</i>&nbsp;
<b class="nc"><i>53</i>&nbsp;        Resource file = storageService.loadAsResource(filename);</b>
<b class="nc"><i>54</i>&nbsp;        return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,</b>
<b class="nc"><i>55</i>&nbsp;                &quot;attachment; filename=\&quot;&quot; + file.getFilename() + &quot;\&quot;&quot;).body(file);</b>
<i>56</i>&nbsp;    }
<i>57</i>&nbsp;
<i>58</i>&nbsp;    @PostMapping(&quot;/sendFile&quot;)
<i>59</i>&nbsp;    @ResponseBody
<i>60</i>&nbsp;    public WebSocketResult handleFileUpload(@RequestParam(&quot;file&quot;) MultipartFile file,
<i>61</i>&nbsp;                                   @RequestParam String uid, @RequestParam String to) {
<i>62</i>&nbsp;
<b class="nc"><i>63</i>&nbsp;        if (uid == null || to == null) {</b>
<b class="nc"><i>64</i>&nbsp;            return new WebSocketResult(false);</b>
<i>65</i>&nbsp;        }
<i>66</i>&nbsp;
<b class="nc"><i>67</i>&nbsp;        String FilePath = storageService.store(file);</b>
<i>68</i>&nbsp;
<b class="nc"><i>69</i>&nbsp;        Message message1 = new Message();</b>
<b class="nc"><i>70</i>&nbsp;        message1.setContent(MySerializable.object_bytes(FilePath));</b>
<b class="nc"><i>71</i>&nbsp;        message1.setFrom(uid);</b>
<b class="nc"><i>72</i>&nbsp;        message1.setTo(to);</b>
<b class="nc"><i>73</i>&nbsp;        message1.setType(&quot;img&quot;);</b>
<i>74</i>&nbsp;
<b class="nc"><i>75</i>&nbsp;        ClientWeb clientWeb = WebPush.clients2.get(uid);</b>
<b class="nc"><i>76</i>&nbsp;        if(null!=clientWeb) {</b>
<b class="nc"><i>77</i>&nbsp;            clientWeb.send(message1);</b>
<b class="nc"><i>78</i>&nbsp;            return new WebSocketResult(true);</b>
<i>79</i>&nbsp;        }
<b class="nc"><i>80</i>&nbsp;        return new WebSocketResult(false);</b>
<i>81</i>&nbsp;    }
<i>82</i>&nbsp;
<i>83</i>&nbsp;    @ExceptionHandler(StorageFileNotFoundException.class)
<i>84</i>&nbsp;    public ResponseEntity&lt;?&gt; handleStorageFileNotFound(StorageFileNotFoundException exc) {
<b class="nc"><i>85</i>&nbsp;        return ResponseEntity.notFound().build();</b>
<i>86</i>&nbsp;    }
<i>87</i>&nbsp;
<i>88</i>&nbsp;    @RequestMapping(&quot;modifyGroupAvatar&quot;)
<i>89</i>&nbsp;    @ResponseBody
<i>90</i>&nbsp;    public WebSocketResult modifyGroupAvatar(@RequestParam String groupid, @RequestParam(&quot;file&quot;) MultipartFile file, @RequestParam String uid) {
<i>91</i>&nbsp;
<b class="fc"><i>92</i>&nbsp;        String FilePath = storageService.store(file);</b>
<i>93</i>&nbsp;
<b class="fc"><i>94</i>&nbsp;        String request = &quot;{&#39;groupid&#39;:&#39;&quot; + groupid + &quot;&#39;,&#39;groupAvatar&#39;:&#39;&quot; + FilePath + &quot;&#39;,&#39;uid&#39;:&#39;&quot; + uid + &quot;&#39;}&quot;;</b>
<b class="fc"><i>95</i>&nbsp;        boolean isSuccess = IkariUtils.sendViWebPush(uid, &quot;sys_groups&quot;, &quot;modifyGroupAvatar&quot;, request);</b>
<b class="fc"><i>96</i>&nbsp;        return new WebSocketResult(isSuccess);</b>
<i>97</i>&nbsp;    }
<i>98</i>&nbsp;
<i>99</i>&nbsp;    @RequestMapping(&quot;modifyUserAvatar&quot;)
<i>100</i>&nbsp;    @ResponseBody
<i>101</i>&nbsp;    public WebSocketResult modifyUserAvatar(@RequestParam(&quot;file&quot;) MultipartFile file, @RequestParam String uid) {
<i>102</i>&nbsp;
<b class="fc"><i>103</i>&nbsp;        String FilePath = storageService.store(file);</b>
<i>104</i>&nbsp;
<b class="fc"><i>105</i>&nbsp;        String request = &quot;{&#39;UserAvatar&#39;:&#39;&quot; + FilePath + &quot;&#39;,&#39;uid&#39;:&#39;&quot; + uid + &quot;&#39;}&quot;;</b>
<b class="fc"><i>106</i>&nbsp;        boolean isSuccess = IkariUtils.sendViWebPush(uid, &quot;sys_groups&quot;, &quot;modifyUserAvatar&quot;, request);</b>
<b class="fc"><i>107</i>&nbsp;        return new WebSocketResult(isSuccess);</b>
<i>108</i>&nbsp;    }
<i>109</i>&nbsp;
<i>110</i>&nbsp;    @RequestMapping(&quot;sendPicToOne&quot;)
<i>111</i>&nbsp;    @ResponseBody
<i>112</i>&nbsp;    public WebSocketResult sendPicToOne(@RequestParam(&quot;file&quot;) MultipartFile file,
<i>113</i>&nbsp;                               @RequestParam String from,
<i>114</i>&nbsp;                               @RequestParam String to,
<i>115</i>&nbsp;                               @RequestParam String time) {
<b class="fc"><i>116</i>&nbsp;        String FilePath = storageService.store(file);</b>
<b class="fc"><i>117</i>&nbsp;        String request = &quot;{&#39;content&#39;:&#39;&quot; + FilePath + &quot;&#39;, &#39;from&#39;:&#39;&quot; + from + &quot;&#39;, &#39;to&#39;:&#39;&quot; + to + &quot;&#39;, &#39;time&#39;:&#39;&quot; + time + &quot;&#39;}&quot;;</b>
<b class="fc"><i>118</i>&nbsp;        boolean isSuccess = IkariUtils.sendViWebPush(from, to, &quot;msg_pic&quot;, request);</b>
<b class="fc"><i>119</i>&nbsp;        return new WebSocketResult(isSuccess);</b>
<i>120</i>&nbsp;    }
<i>121</i>&nbsp;
<i>122</i>&nbsp;    @RequestMapping(&quot;sendPicToGroup&quot;)
<i>123</i>&nbsp;    @ResponseBody
<i>124</i>&nbsp;    public WebSocketResult sendPicToGroup(@RequestParam(&quot;file&quot;) MultipartFile file,
<i>125</i>&nbsp;                                 @RequestParam String uid,
<i>126</i>&nbsp;                                 @RequestParam String gid,
<i>127</i>&nbsp;                                 @RequestParam String time) {
<b class="fc"><i>128</i>&nbsp;        String FilePath = storageService.store(file);</b>
<b class="fc"><i>129</i>&nbsp;        String request = &quot;{&#39;content&#39;:&#39;&quot; + FilePath + &quot;&#39;, &#39;uid&#39;:&#39;&quot; + uid + &quot;&#39;, &#39;gid&#39;:&#39;&quot; + gid + &quot;&#39;, &#39;time&#39;:&#39;&quot; + time + &quot;&#39;}&quot;;</b>
<b class="fc"><i>130</i>&nbsp;        boolean isSuccess = IkariUtils.sendViWebPush(uid, &quot;sys_groups&quot;, &quot;groupMsg_pic&quot;, request);</b>
<b class="fc"><i>131</i>&nbsp;        return new WebSocketResult(isSuccess);</b>
<i>132</i>&nbsp;    }
<i>133</i>&nbsp;
<i>134</i>&nbsp;
<i>135</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2018-04-18 16:28</div>
</div>
</body>
</html>
